<template>
	<view class="customerForm">
		<uni-forms ref="baseForm" :modelValue="formData" label-width="90px">
			<uni-forms-item label="分类名称" required>
				<uni-easyinput v-model="formData.Name" placeholder="请输入分类名称" />
			</uni-forms-item>
			<!-- <uni-forms-item label="分类图片">
				<uni-file-picker ref="ufile" v-model="iconList" fileMediatype="image" mode="grid" @select="select"
					limit="1" @delete="delImage" />
			</uni-forms-item> -->
		</uni-forms>
		<button type="primary" @click="submit">保 存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {},
				iconList: []
			}
		},
		onLoad(query) {
			if (query.Id) {
				this.getTheData(query.Id)
				uni.setNavigationBarTitle({
					title: '编辑分类'
				})
			}
			this.formData.CompanyId = uni.getStorageSync('CompanyInfo').Id
		},
		methods: {
			// 删除图片
			delImage(e) {
				this.formData.Icon = ''
			},
			// 上传
			select(e) {
				uni.uploadFile({
					url: this.$baseUrl + '/Base_Manage/Upload/Filleimag',
					filePath: e.tempFilePaths[0],
					header: {
						'Authorization': uni.getStorageSync('tokenKey')
					},
					name: 'file',
				}).then(result => {
					let [err, res] = [...result]
					if (err !== null) {
						uni.showToast({
							icon: 'none',
							title: '网络连接失败,请重试'
						})
						this.$refs.ufile.clearFiles()
						return
					}
					this.formData.Icon = JSON.parse(res.data).Data
				})
			},
			getTheData(id) {
				this.$http("post", "/BO/InventType/GetTheData", {
					id
				}).then((res) => {
					if (res.Success) {
						this.formData = res.Data;
						if (this.formData.Icon) {
							this.iconList = [{
								url: this.formData.Icon
							}]
						}
					} else uni.showToast({
						icon: 'none',
						title: res.Msg
					})
				});
			},
			submit() {
				if (!this.formData.Name) return this.$api.msg('请填写分类名称')
				uni.showLoading()
				this.$http('post', "/BO/InventType/saveData", this.formData)
					.then((res) => {
						uni.hideLoading()
						if (res.Success) {
							uni.showToast({
								icon: 'none',
								title: '保存成功'
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
					});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.customerForm {
		padding: 20rpx;
		box-sizing: border-box;
	}
</style>